﻿Imports System.Data.SqlClient

Public Class adm_productpurchase
    Inherits System.Web.UI.Page
    Private sSQL As String
    Private objrd As SqlDataReader = Nothing
    Private objfind As New GetData
    Private ps As New PreparedStatement(sSQL)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Session("admin_ID") <> Nothing And Session("admin_ID") <> "") Then
            If (Request("uid") <> Nothing And Request("uid") <> "") Then
                Dim memberId, strErr As String
                Dim companyId As String = ""
                Dim id, title, purchaseCategory, purchaseSubCategory, description, country, state, city, delplace, purchaseNum, expPrice, linkMan As String
                Dim validdate
                Dim gender As Boolean
                strErr = ""
                memberId = Request("uid")
                If (Request("submitpurchase") <> Nothing And Request("submitpurchase") <> "") Then
                    If (Request("c_purchase_title") = "" Or Request("c_purchase_title") = Nothing) Then
                        Me.c_purchase_title_label.Text = "please enter title！"
                        Exit Sub
                    Else
                        Me.c_purchase_title_label.Text = ""
                    End If
                    If (Request("c_linkman") = "" Or Request("c_linkman") = Nothing) Then
                        Me.c_linkman_label.Text = "please enter contact！"
                        Exit Sub
                    Else
                        Me.c_linkman_label.Text = ""
                    End If
                    ps.strSql = "select * from merchant where owner_id =@ownerId"
                    ps.addParameter("ownerId", memberId)
                    Dim conn As SqlConnection = objfind.getConn()

                    objrd = objfind.dr(ps, strErr, conn, objrd)
                    If (objrd.Read()) Then
                        companyId = objrd.Item("company_id")
                    End If
                    id = System.Guid.NewGuid().ToString()
                    title = Request("c_purchase_title")
                    purchaseCategory = Request("c_purchase_category")
                    If (Request("c_purchase_subcategory") <> Nothing And Request("c_purchase_subcategory") <> "") Then
                        purchaseSubCategory = Request("c_purchase_subcategory")
                    Else
                        purchaseSubCategory = ""
                    End If
                    description = Request("c_description")
                    country = Request("country")
                    state = Request("state")
                    city = Request("city")
                    delplace = Request("c_delplace")
                    purchaseNum = Request("c_purchaseNum")
                    expPrice = Request("c_expprice")
                    If (Request("c_validdate") = Nothing Or Request("c_validdate").Trim = "") Then
                        validdate = "1753-01-01"
                    Else
                        validdate = Request("c_validdate")
                    End If
                    linkMan = Request("c_linkman")
                    If (Request("gender") = "0") Then
                        gender = False
                    ElseIf (Request("gender") = "1") Then
                        gender = True
                    End If
                    ps.strSql = "insert into purchase(id,company_id,title,category_id,sub_category_id,detailinfo,country,state,city,delplace,purchasenum,expprice,validdate,linkman,gender,lang_flag,create_date)" & _
                        "values(@id,@companyId,@title,@category,@subCategory,@detailInfo,@country,@state,@city,@delplace,@purchasenum,@expPrice,@validdate,@linkman,@gender,'en',@createDate)"
                    ps.parameters.Clear()
                    ps.addParameter("id", id)
                    ps.addParameter("companyId", companyId)
                    ps.addParameter("title", title)
                    ps.addParameter("category", purchaseCategory)
                    ps.addParameter("subCategory", purchaseSubCategory)
                    ps.addParameter("detailInfo", description)
                    ps.addParameter("country", country)
                    ps.addParameter("state", state)
                    ps.addParameter("city", city)
                    ps.addParameter("delplace", delplace)
                    ps.addParameter("purchasenum", purchaseNum)
                    ps.addParameter("expPrice", expPrice)
                    ps.addParameter("validdate", validdate)
                    ps.addParameter("linkman", linkMan)
                    ps.addParameter("gender", gender)
                    ps.addParameter("createDate", Now)
                    objfind.es(ps, strErr)
                    objfind.closeConn(conn)

                    Response.Redirect("adm_purchaseManagement.aspx?uid=" & Request("uid"))
                Else
                    ps.strSql = "select * from category where lang_flag='en'"
                    ps.parameters.Clear()
                    Dim conn As SqlConnection = objfind.getConn()

                    objrd = objfind.dr(ps, strErr, conn, objrd)
                    While (objrd.Read())
                        c_purchase_category.Items.Add(New ListItem(
                        objrd("Category_Name").ToString(), objrd("Category_ID").ToString()))
                    End While
                    objfind.closeConn(conn)

                End If
            Else
                Response.Redirect("../memberMngt_default.aspx")
            End If
        Else
            Response.Redirect("../adm_login.aspx")
        End If
    End Sub

    Protected Sub c_purchase_category_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles c_purchase_category.SelectedIndexChanged
        Dim categoryId, strErr As String
        strErr = ""
        categoryId = c_purchase_category.SelectedValue
        ps.strSql = "select * from sub_category where category_id=@categoryId and lang_flag='en'"
        ps.parameters.Clear()
        ps.addParameter("categoryId", categoryId)
        Dim conn As SqlConnection = objfind.getConn()

        objrd = objfind.dr(ps, strErr, conn, objrd)
        c_purchase_subcategory.Items.Clear()
        c_purchase_subcategory.Items.Add(New ListItem("select sub category", ""))
        While (objrd.Read())
            c_purchase_subcategory.Items.Add(New ListItem(
            objrd("Sub_Category_Name").ToString(), objrd("Sub_Category_ID").ToString()))
        End While
        c_purchase_category.Items.Clear()
        c_purchase_category.Items.Add(New ListItem("select category", ""))
        ps.strSql = "select * from category where lang_flag='en'"
        ps.parameters.Clear()
        objrd = objfind.dr(ps, strErr, conn, objrd)
        While (objrd.Read())
            c_purchase_category.Items.Add(New ListItem(
            objrd("Category_Name").ToString(), objrd("Category_ID").ToString()))
        End While
        objfind.closeConn(conn)

        c_purchase_category.SelectedValue = categoryId
    End Sub

End Class